<?php
namespace app\admin\controller;
use think\Controller;

use app\admin\model\Setting as SettingModel;
use app\admin\model\Businesscenter as BCModel;
use think\Session;

class Setting extends AdminBase{


    //设置页面展示
	public function index(){

        $UserID = Session::get('userid');
        $UserName = Session::get('username');

        $UserStatus = Session::get('userstatus');
            if($UserStatus != 2){
               
                //不是管理员
                $this->error('ERROR!!!!');
            }


        //dump($UserID);die;
        //初始化模型
        $UserSetting = new SettingModel;
        $data = $UserSetting->where('id',1)->find();

        $this->assign('data',$data);




        $this->assign('username',$UserName);
        $this->assign('userid',$UserID);


        // dump($data);die;

        return $this->fetch();
    }
    public function indexForm(){
    	$UserInfo = input('post.');

        //存入
        //初始化模型
        //dump($UserInfo);die;

        $AdminSetting = new SettingModel();

       $Reslut = $AdminSetting->save([
            'sell_coin' => $UserInfo['sell_coin'],
            'limit_deallist_money' => $UserInfo['limit_deallist_money'],
            'block_percent' => $UserInfo['block_percent'],
            'deal_list_back' => $UserInfo['deal_list_back'],
            'deal_list_for_other' => $UserInfo['deal_list_for_other'],
            'withdrawlist_percent' => $UserInfo['withdrawlist_percent'],
            'first_percent' => $UserInfo['first_percent'],
            'second_percent' => $UserInfo['second_percent']
            
        ],[
            'id' => 1
        ]);

       
        //return $this->redirect('Come/index',array('id' => $data['id'],'username' => $data['username']));
        if($Reslut){
            //修改成功
            //更新所有的报单中心表的比例
            $UserBCModel = new BCModel();
            //更新一级报单中心比例
            $UserBCModelLevel1 = $UserBCModel->save([

                'Business_percent' => $UserInfo['first_percent']

            ],['status' => 1, 'Business_level' => 1]);

            $UserBCModelLevel2 = $UserBCModel->save([

                'Business_percent' => $UserInfo['second_percent']

            ],['status' => 1, 'Business_level' => 2]);



            if($UserBCModelLevel1 || $UserBCModelLevel2){
                $this->success('修改成功!');
            }

            //$this->success('修改成功!');
        }else{
            //修改失败

            $this->error('修改失败',"Setting/index");
        }
    }
}